<?php

namespace Api\Controller;

use Common\Controller\ApiController;

/**
 * 基地接口
 */
class BasesController extends ApiController {

    /**
     * 基地列表
     */
    public function baseList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'id');
        $orderDirection = I('post.orderDirection', 'DESC');

        //构造WHERE条件
        $where = '1=1 AND status=1';

        $base = M('Base');
        //数据总数
        $total = $base->where($where)->count();

        //数据列表
        $list = $base->where($where)
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();

        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['add_time'] = !empty($item['add_time']) ? date('Y-m-d H:i:s', intval($item['add_time'])) : '';
                $item['upd_time'] = !empty($item['upd_time']) ? date('Y-m-d H:i:s', intval($item['upd_time'])) : '';
            }
            $data['total'] = $total;
            $data['pages'] = ceil($total / $pageSize);
            $data['list'] = $list;
            apiReturn('获取数据', 200, $data);
        } else {
            apiReturn('无数据', 300);
        }
    }

    /**
     * 获取基地下地块信息
     */
    public function getBlocks() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'id');
        $orderDirection = I('post.orderDirection', 'DESC');
        $base_id = I('post.base_id', 0);
        $block = M('Block');

        if (empty($base_id)) {
            apiReturn('基地ID不能为空', 300);
        }

        //构造WHERE条件
        $where = "1=1 AND status=1 AND base_id={$base_id}";

        //数据总数
        $total = $block->where($where)->count();

        //数据列表
        $list = $block->field('id,sn,area')
                ->where($where)
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();

        if (!empty($list)) {
            //获取苗木分类种类信息
            $treeTypeInfo = R('Tree/getTreeTypeInfo');

            foreach ($list as &$item) {
                $countInfoArr = array();
                $countInfoRet = M('Tree')->field('type_id,status,COUNT(*) AS num')->where("block_id={$item['id']}")->group('type_id,status')->select();
                if (!empty($countInfoRet)) {
                    foreach ($countInfoRet as $countInfo) {
                        if (!array_key_exists($countInfo['type_id'], $countInfoArr)) {
                            $tmp = $treeTypeInfo[$countInfo['type_id']];
                            $countInfoArr[$countInfo['type_id']] = array(
                                'large_name' => $tmp['large_name'],
                                'small_name' => $tmp['small_name'],
                            );
                        }
                        if ($countInfo['status'] == 1) {
                            $countInfoArr[$countInfo['type_id']]['save'] = $countInfo['num'];
                        } else if ($countInfo['status'] == 2) {
                            $countInfoArr[$countInfo['type_id']]['out'] = $countInfo['num'];
                        } else if ($countInfo['status'] == 3) {
                            $countInfoArr[$countInfo['type_id']]['die'] = $countInfo['num'];
                        }
                    }
                }
                $item['countInfo'] = $countInfoArr;
            }
            unset($item);

            $data['total'] = $total;
            $data['pages'] = ceil($total / $pageSize);
            $data['list'] = $list;
            apiReturn('获取数据', 200, $data);
        } else {
            apiReturn('无数据', 300);
        }
    }

    /**
     * 获取基地列表信息
     */
    public function getBases() {
        $list = M('Base')->field('id,name')->where("status=1")->select();
        if (!empty($list)) {
            apiReturn('获取数据', 200, $list);
        } else {
            apiReturn('无数据', 300);
        }
    }
}
